package com.xoa.controller.cms;

import com.xoa.model.cms.CmsChnldoc;
import com.xoa.model.cms.CmsDocumentInfoWithBLOBs;
import com.xoa.model.cms.DocumentInfoModel;
import com.xoa.service.cms.CmsDocumentService;
import com.xoa.util.common.wrapper.BaseWrapper;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("cmsDocument")
public class CmsDocumentController {
    @Autowired
    private CmsDocumentService documentService;


    @ResponseBody
    @RequestMapping("/datagrid")
    public BaseWrapper datagrid(Integer channelId, boolean useFlag, Integer pageSize, Integer page){
        return documentService.datagrid(channelId, useFlag,pageSize,page);
    }

    @ResponseBody
    @RequestMapping("/addDocument")
    public BaseWrapper addDocument(HttpServletRequest request, DocumentInfoModel documentInfoModel){
        BaseWrapper baseWrapper=new BaseWrapper();
        CmsChnldoc doc =documentService.addDocument(request,documentInfoModel);
        baseWrapper.setData(doc);
        baseWrapper.setFlag(true);
        baseWrapper.setMsg("ok");
        baseWrapper.setStatus(true);
        return baseWrapper;
    }

    @ResponseBody
    @RequestMapping("/updateDocument")
    public BaseWrapper updateDocument(DocumentInfoModel documentInfoModel, HttpServletRequest request){
        BaseWrapper baseWrapper=new BaseWrapper();
        documentService.updateDocument(documentInfoModel,request);
        baseWrapper.setFlag(true);
        baseWrapper.setStatus(true);
        return baseWrapper;
    }

    @ResponseBody
    @RequestMapping("/deleteDocumentByChnlDoc")
    public BaseWrapper deleteDocumentByChnlDoc(@RequestParam(required = false, value = "docids[]") List<Integer> docids){
            return documentService.deleteDocumentByChnlDoc(docids);
    }

    @ResponseBody
    @RequestMapping("/getDocument")
    public BaseWrapper getDocument(int docId){
        BaseWrapper baseWrapper = new BaseWrapper();

        CmsDocumentInfoWithBLOBs documentByDocId = documentService.getDocumentByDocId(docId);

        baseWrapper.setData(documentByDocId);
        baseWrapper.setFlag(true);
        baseWrapper.setMsg("ok");
        baseWrapper.setStatus(true);
        return baseWrapper;
    }

    @ResponseBody
    @RequestMapping("/getChnlDoc")
    public BaseWrapper getChnlDoc(int chnlId, int docId){
        BaseWrapper baseWrapper =new BaseWrapper();
       baseWrapper.setData(documentService.getChnlDoc(chnlId,docId));
        baseWrapper.setFlag(true);
        baseWrapper.setStatus(true);
        return baseWrapper;
    }

    @ResponseBody
    @RequestMapping("/moveToTrash")
    public BaseWrapper moveToTrash( @RequestParam(required = false, value = "docids[]") List<Integer> docids){
        BaseWrapper json = new BaseWrapper();
        documentService.moveToTrash(docids);
        json.setStatus(true);
        json.setFlag(true);
        return json;
    }

    @ResponseBody
    @RequestMapping("/clearTrash")
    public BaseWrapper clearTrash(Integer channelId){
        BaseWrapper baseWrapper = new BaseWrapper();
        return documentService.clearTrash(channelId);
       
    }

    @ResponseBody
    @RequestMapping("/recycle")
    public BaseWrapper recycle(@RequestParam(required = false, value = "docids[]") List<Integer> docids){
        BaseWrapper baseWrapper=new BaseWrapper();
        int i=documentService.recycle(docids);
        if (i>0) {
            baseWrapper.setFlag(true);
            baseWrapper.setMsg("ok");
            baseWrapper.setStatus(true);
        }
        return  baseWrapper;
    }

    @ResponseBody
    @RequestMapping("/datagridTrash")
    public BaseWrapper datagridTrash(Integer channelId, boolean useFlag, Integer pageSize, Integer page){
        return documentService.datagridTrash(channelId,useFlag,pageSize,page);
    }

    /**
     * @作者: 张航宁
     * @时间: 2019/5/24
     * @说明: 更新文档状态
     */
    @ResponseBody
    @RequestMapping("/updateCmsDocumentStatus")
    public BaseWrapper updateCmsDocumentStatus(CmsDocumentInfoWithBLOBs documentInfoModel){
        return  documentService.updateCmsDocumentStatus(documentInfoModel);
    }

    /**
     * @作者: 张航宁
     * @时间: 2019/5/27
     * @说明: 根据栏目id查询该栏目下的已发布的文章 带分页 可扩展
     */
    @ResponseBody
    @RequestMapping("/selCmsDocs")
    public BaseWrapper selCmsDocs(Integer chnlId , PageParams pageParams){
        return documentService.selCmsDocs(chnlId ,pageParams);
    }

    /**
     * @作者: 张航宁
     * @时间: 2019/6/3
     * @说明: 获取首页轮播文章（暂时查询五个）
     */
    @ResponseBody
    @RequestMapping("/selCarousel")
    public BaseWrapper selCarousel(){
        return documentService.selCarousel();
    }
}
